package com.lzy.cloud.goods.controller;

import com.lzy.cloud.goods.dtos.CarParamAppDto;
import com.lzy.cloud.goods.dtos.CarParamDto;
import com.lzy.cloud.goods.dtos.ParamDto;
import com.lzy.cloud.goods.facade.ParamFacade;
import com.lzy.cloud.goods.request.ParamRequest;
import com.lzy.cloud.goods.service.ParamService;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 基本参数
 *
 * @author lc
 * @version 2018/7/30
 */
@RestController
@RequestMapping("/param")
public class ParamController {

    @Autowired
    private ParamService paramService;

    @Autowired
    private ParamFacade paramFacade;

    /**
     * 平台端的增加
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result addParam(@RequestBody ParamRequest paramRequest) {
        Ensure.that(paramRequest).isNotNull("00000002");
        ParamDto paramDto = DozerUtils.map(paramRequest, ParamDto.class);
        paramDto.setType(1);
        Boolean flag  = paramService.add(paramDto)>0;
        Ensure.that(flag).isTrue("00000003");
        return new Result(flag);
    }

    /**
     * 平台端的刪除
     */
    @RequestMapping(value = "/delete/{ids}", method = RequestMethod.GET)
    public Result deleteParam(@PathVariable("ids") String ids) {
        Ensure.that(ids).isNotNull("00000002");
        Boolean flag = paramFacade.deleteParams(ids);
        Ensure.that(flag).isTrue("00000005");
        return new Result(flag);
    }

    /**
     * 平台端的修改
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result update(@RequestBody ParamRequest paramRequest) {
        Ensure.that(paramRequest).isNotNull("00000002");
        ParamDto paramDto = DozerUtils.map(paramRequest, ParamDto.class);
        paramDto.setType(1);
        Boolean flag = paramService.update(paramDto);
        Ensure.that(flag).isTrue("00000004");
        return new Result(flag);
    }

    /**
     * 条件查询
     */
    @RequestMapping(value = "/find", method = RequestMethod.GET)
    public Result selectList(ParamRequest paramRequest) {
        List<ParamDto> paramDtos = paramService.selectList(DozerUtils.map(paramRequest,ParamDto.class));
        Ensure.that(paramDtos).isNotNull("00000006");
        return new Result(paramDtos);
    }

    /**
     * 查询具体车型核心参数及值
     */
    @RequestMapping(value = "/detail/core/{id}", method = RequestMethod.GET)
    public Result selectCore(@PathVariable("id") String id) {
            List<CarParamDto> carParamDtos = paramFacade.selectCore(id);
        Ensure.that(carParamDtos).isNotNull("00000006");
        return new Result(carParamDtos);
    }

    /**
     * 根据sourceId得到核心参数,及 值
     */
    @RequestMapping(value = "/find/core/{sourceId}", method = RequestMethod.GET)
    public Result findCore(@PathVariable("sourceId") Long sourceId) {
        List<CarParamAppDto> carParamDtos = paramFacade.findCore(sourceId);
        Ensure.that(carParamDtos).isNotNull("00000006");
        return new Result(carParamDtos);
    }
}
